
;
var BASE = BASE || {};
(function($, $$) {

    $$.Widget = function(self, options) {
        var widget = this;
        if (!(widget instanceof $$.Widget)) {   //instanceof  获取一个对象是否是数组，或判断某个变量是否是某个对象的实例
            return new $$.Widget(options);
        }
        var defaults = {
            widgetKey : null,
            animateTime : $$.defaultAnimateDuration,
            disabled : false,
            minPosition : "bottom",
            clone : null
        };
        widget.jqObj = self;
        widget.opts = {};
        $.extend(widget.opts, defaults, options);
    };
    $$.Widget.prototype = {
        constructor : $$.Widget,
        selectHandle : function() {
            var args = Array.prototype.slice.call(arguments), widgetObj = this, func = args.shift();
            if ($.isFunction(widgetObj[func])) {
                return widgetObj[func].apply(widgetObj, args);
            }
        },
        self : function() {
            return this.jqObj;
        },

        createWidget : function() {
            var widgetObj = this;
            var opts = widgetObj.opts;
            var jqObj = widgetObj.jqObj;
            opts.clone = jqObj.clone();
            opts.widgetKey = $$.getRandomKey();
            jqObj.attr("widget", opts.widgetKey);
            widgetObj.widget(opts.widgetKey, widgetObj);
            return widgetObj;
        },

        widget : function() {
            if (arguments.length == 2) {
                return $$.addWidget(arguments[0], arguments[1]);
            }
            else
            if (arguments.length == 1) {
                return $$.getWidget(arguments[0]);
            }
            return this;
        },

        removeWidget : function(key) {
            if (key !== undefined) {
                $$.removeWidget(key);
            }
        },

        init : function() {

        },

        min : function(self, opts, target, e) {
            var widgetObj = this;
            if ($.isFunction(opts.beforeMin)) {
                if (opts.beforeMin.call(self[0], target, e) === false) {
                    return self;
                }
            }
            $(">.uiTitleBar .uiMinBtn", self).removeClass("uiMinBtn").addClass("uiResumeBtn");
            opts.minStatusHeight = self.find("> .uiTitleBar").outerHeight(true);
            opts.originHeight = self.height();
            opts.originWidth = self.width();
            if (opts.resizable) {
                $(".uiResizable", self).hide();
            }
            var positionStr = self.css("position");
            var animateTime = opts.animateTime || $$.defaultAnimateDuration;
            if (positionStr === "static" || positionStr === "relative") {
                opts.overflowStatus = self.css("overflow");
                self.css("overflow", "hidden").animate({height : opts.minStatusHeight}, animateTime);
            }
            else {
                var oldPos = {position : null, left : null, top : null, right : null, bottom : null};
                $.each(oldPos, function(key) {
                    oldPos[key] = self.css(key);
                });
                if (positionStr == "absolute") {
                    if (oldPos["top"] == "auto") {
                        oldPos["top"] = self.offset().top;
                    }
                    oldPos["top"] = parseInt(oldPos["top"]) - $(document).scrollTop();
                }
                opts.originPosition = oldPos;
                positionStr = "fixed";

                if ($.browser.msie && $.browser.version == "6.0") {
                    positionStr = "absolute";
                }
                var temOffset = self.offset();
                var animateOptions = {height : opts.minStatusHeight, width : opts.minStatusWidth, left : 0, top : 0};
                if (opts.minPosition == "bottom") {
                    animateOptions.top = $(window).height() - opts.minStatusHeight;
                }
                self.css({right : null, bottom : null, position : positionStr, left : temOffset.left, top : temOffset.top}).animate(animateOptions, animateTime);
            }
            if ($.isFunction(opts.min)) {
                if (opts.min.call(self[0], target, e) === false) {
                    return self;
                }
            }
            return widgetObj;
        },

        resume : function(self, opts, target, e) {
            var widgetObj = this;
            if ($.isFunction(opts.beforeResume)) {
                if (opts.beforeResume.call(self[0], target, e) === false) {
                    return self;
                }
            }
            $(">.uiTitleBar .uiResumeBtn", self).removeClass("uiResumeBtn").addClass("uiMinBtn");
            if (opts.resizable) {
                $(".uiResizable", self).show();
            }
            var positionStr = self.css("position");
            if (positionStr === "static" || positionStr === "relative") {
                self.height(opts.originHeight).width(opts.originWidth).css("overflow", opts.overflowStatus);
            }
            else {
                if (opts.originPosition["position"] == "absolute") {
                    opts.originPosition["top"] = parseInt(opts.originPosition["top"]) + $(document).scrollTop();
                }
                var tmpSetting = $.extend({}, opts.originPosition, {height : opts.originHeight, width : opts.originWidth});
                self.css(tmpSetting);
            }
            if ($.isFunction(opts.resume)) {
                if (opts.resume.call(self[0], target, e) === false) {
                    return self;
                }
            }
            return widgetObj;
        },

        close : function(self, opts, target, e) {
            var widgetObj = this;
            if ($.isFunction(opts.beforeClose)) {
                if (opts.beforeClose.call(self[0], target, e) === false) {
                    return self;
                }
            }
            if (opts.modal) {
                if ($.browser.msie && $.browser.version == "6.0") {
                    opts.selectList.css("visibility", "visible");
                }
                $(".uiMask").remove();
            }
            var animateTime = opts.animateTime || $$.defaultAnimateDuration;
            var animateFunc = opts.closeAnimate || "slideUp";
            self[animateFunc](animateTime, function() {
                if ($.isFunction(opts.close)) {
                    if (opts.close.call(self[0], target, e) === false) {
                        return self;
                    }
                }
                if (opts.destroyOnClose) {
                    widgetObj.destroy();
                }
            });
            return widgetObj;
        },

        open : function() {
            var widgetObj = this, self = widgetObj.jqObj, opts = widgetObj.opts;
            if ($.isFunction(opts.beforeOpen)) {
                if (opts.beforeOpen.call(self) === false) {
                    return self;
                }
            }
            if (self.css("position") != "static") {
                if (opts.active) {
                    self.siblings(".uiDialog").each(function() {
                        var obj = $(this);
                        if (obj.css("position") != "static") {
                            obj.css("z-index", opts.zIndex).children(".uiTitleBar").addClass("uiInactive");
                        }
                    });
                }
            }

            if (opts.modal) {
                if ($(".uiMask").length == 0) {
                    $("<div />").addClass("uiMask").appendTo("body");
                }
            }
            var animateTime = opts.animateTime || $$.defaultAnimateDuration;
            var animateFunc = opts.openAnimate || "slideDown";
            self[animateFunc](animateTime, function() {
                if ($.isFunction(opts.open)) {
                    opts.open.call(self);
                }
            });
            return widgetObj;
        },
        disable : function() {
            var self = this, opts = self.opts;
            opts.disabled = true;
            return self.addClass("uiWidgetDisalbed");
        },

        enable : function() {
            var self = this, opts = self.opts;
            opts.disabled = false;
            return self.removeClass("uiWidgetDisalbed");
        },
        destroy : function(revert) {

            var widgetObj = this, self = widgetObj.jqObj, opts = widgetObj.opts;
            self.find(".uiWidget").each(function() {
                var obj = $(this), key = obj.attr("widget"), widget = widgetObj.widget(key);
                if (widget != null) {
                    widgetDestroy(widget, revert);
                }
            });
            widgetDestroy(widgetObj, revert);
        }
    };

    function widgetDestroy(widget, revert) {
        var self = widget.jqObj, opts = widget.opts;
        var swidgetKey;
        if (!self.hasClass("uiWidget")) {
            self = opts.targetWidget;
        }
        if (self.hasClass("uiDraggable") || self.find(".uiDraggable").length != 0) {
            self.draggable("destroy");
        }
        if (self.find(".uiResizable").length != 0) {
            self.resizable("destroy");
        }
        if ($.isFunction(widget.beforeDestroy)) {
            widget.beforeDestroy();
        }
        widgetKey = self.removeClass("uiWidget").attr("widget");
        widget.removeWidget(widgetKey);
        if (revert) {
            opts.clone.insertAfter(self);
        }
        self.remove();
        opts.clone = null;
        opts = null;
    }
})(jQuery, BASE);
